package org.papervision3d.core.animation.track
{
	import org.papervision3d.core.animation.keyframe.AbstractKeyFrame;
	import org.papervision3d.core.math.Matrix3D;
	import org.papervision3d.objects.DisplayObject3D;
	
	public class AbstractTrack
	{
		public var output :Array;
		public var target :DisplayObject3D;
		public var keyFrames :Array;
		public var startTime :Number;
		public var endTime :Number;
		public var current :AbstractKeyFrame;
		public var next :AbstractKeyFrame;
		public var alpha :Number = 0;
		
		public var matrix :Matrix3D;
		
		public function AbstractTrack(target:DisplayObject3D)
		{
			this.target = target;
			this.startTime = Number.MAX_VALUE;
			this.endTime = -this.startTime;
			this.keyFrames = new Array();
			this.matrix = Matrix3D.IDENTITY;
		}
		
		/**
		 * 
		 */ 
		public function addKeyFrame(keyframe:AbstractKeyFrame):void
		{
			this.output = this.output || new Array(keyframe.data.length);
			this.startTime = Math.min(this.startTime, keyframe.time);
			this.endTime = Math.max(this.endTime, keyframe.time);
			this.keyFrames.push(keyframe);
		}
		
		/**
		 * 
		 */ 
		public function tick(seconds:Number):void
		{
			var kfs :Array = this.keyFrames;
			var numKfs :int = kfs.length;
			var duration : Number = 0;
			var i :int;
			
			current = next = null;
			alpha = 0;
			
			if(numKfs == 0)
			{
				return;
			}
			else if(numKfs == 1 || seconds < startTime)
			{
				current = kfs[0];
			}
			else if(seconds > endTime)
			{
				current = kfs[numKfs-1];
			}
			else
			{
				for(i = 0; i < numKfs - 1; i++)
				{
					current = kfs[i];	
					next = kfs[i+1];			
					if(next.time > seconds)
						break;
				}	
				
				duration = next.time - current.time;
				alpha = (seconds - current.time) / duration;
			}
		}
	}
}